package com.my.demo.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.my.demo.config.ZhenZi;
import com.my.demo.pojo.Result;
import com.my.demo.pojo.User;
import com.my.demo.service.UserService;

import com.my.demo.utils.DesUtil;
import com.my.demo.utils.SaltUtils;
import com.my.demo.shiro.token.SmsToken;
import java.util.Map;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
//用户进行注册的Controller
public class RegisterController {
  @Autowired
  UserService userService;
  @Autowired
  ZhenZi zhenZi;
  String PW_PATTERN_1 = "^[0-9A-Za-z]{8,16}$";
//  ^(?![A-Za-z0-9]+$)(?![a-z0-9\\W]+$)(?![A-Za-z\\W]+$)(?![A-Z0-9\\W]+$)[a-zA-Z0-9\\W]{8,}$

  //发送短信的的接口
  @RequestMapping("/sendSms")
  @CrossOrigin
  public Result sendSms(@RequestBody Map<String,String> map) {
    Result result = new Result();
    result.setSuccess(false);
    result.setDetail(null);
    String phone=map.get("userName");


      try {
        JSONObject json = zhenZi.send_message(phone);

        if (json.getString("code").equals("0")) {
          result.setMsg("发送成功");
          result.setSuccess(true);
          result.setDetail(DesUtil.getEncryptString(phone+json.getString("verifyCode")));
        } else {
          result.setMsg("发送失败");
        }

      } catch (Exception e) {
        result.setMsg(e.getMessage());
        e.printStackTrace();
      }
    return result;
  }

  //进行验证码的校验后，提供注册的接口
  @PostMapping("/register")
  @CrossOrigin
  public Result toRegister( @RequestBody Map<String, String> models) {
    Subject subject= SecurityUtils.getSubject();


    Result result = new Result();
    result.setSuccess(false);
    result.setDetail(null);

    User user = new User(models.get("userName"), models.get("password"), models.get("userType"));
    String passWd2 = models.get("passWd2");
    String sms=models.get("verifyCode");
    String token=models.get("detail");


    QueryWrapper<User> wrapper = new QueryWrapper<>();
    wrapper.eq("user_name", user.getUserName());
    try {
      User existUser = userService.getOne(wrapper);
      if (existUser != null) {
        //如果用户名已存在
        result.setMsg("用户名已存在");
      } else if (!passWd2.equals(user.getPassword())) {
        result.setMsg("两次密码不一致");
      } else {
//        if(!passWd2.matches(PW_PATTERN_1)){
//          result.setMsg("注册密码必须为8到16为数字英文");
//          return result;
//        }
        int a=0,b=0,c=0,d=0;
        for(int i=0;i<passWd2.length();i++){
          if(passWd2.charAt(i)<='9'&&passWd2.charAt(i)>='0'){
            a+=1;
          }
          else if(passWd2.charAt(i)<='z'&&passWd2.charAt(i)>='a'){
            b+=1;
          }
          else if(passWd2.charAt(i)<='Z'&&passWd2.charAt(i)>='A'){
            c+=1;
          }else {
            d+=1;
          }
        }
        System.out.println(a);
        System.out.println(b);
        System.out.println(c);
        System.out.println(d);
        if(!(a>=1&&b>=1&&c>=1&&d>=1)){
          result.setMsg("数字，大小写字母，特殊字符必须包含");
          return result;
        }
        System.out.println(a+b+c+d);

        subject.login(new SmsToken(user.getUserName(),sms,token));

        //1.生成随机盐
        String salt = SaltUtils.getSalt(6);
        //2.将随机盐保存到数据
        user.setSalt(salt);
        //3.明文密码进行md5 + salt + hash散列
        Md5Hash md5Hash = new Md5Hash(user.getPassword(),salt,1024);
        user.setPassword(md5Hash.toHex());

        subject.logout();
        userService.save(user);
        result.setSuccess(true);
        result.setMsg("注册成功");

        //注册成功后，移除验证码信息
        result.setDetail(user);
      }
    } catch (Exception e) {
      result.setMsg("验证码错误");
      e.printStackTrace();
    }
    return result;
  }
//  @PostMapping("/bussiness_register")
//  public Result toRegister(HttpSession session, @RequestBody Map<String, String> models) {
//    Result result=new Result();
//    result
//  }
}
